import { createRouter, createWebHashHistory } from 'vue-router'
import login from '@/views/loginView.vue'
import home from '@/views/homeView.vue'

export const routes = [
  {
    path: '/login',
    name: 'login',
    label: '登录',
    component: login
  },
  {
    label: '首页',
    path: '/',
    name: 'home',
    component: home,
    children: [
      {
        label: '账户信息',
        path: 'manager',
        name: 'manager',
        component: () => import('@/views/Manager/ManagerIndexView.vue'),
        children: [
          {
            label: '管理员列表',
            path: 'managerlist',
            name: 'managerlist',
            component: () => import('@/views/Manager/ManagerListView.vue')
          }
        ]
      },
      {
        label: '轮播图管理',
        path: 'banner',
        name: 'banner',
        component: () => import('@/views/Banner/BannerIndexView.vue'),
        children: [
          {
            label: '轮播图列表',
            path: 'bannerlist',
            name: 'bannerlist',
            component: () => import('@/views/Banner/BannerListView.vue')
          },
          {
            label: '添加轮播图',
            path: 'banneradd',
            name: 'banneradd',
            component: () => import('@/views/Banner/BannerAddView.vue')
          }
        ]
      },
      {
        label: '产品管理',
        path: 'production',
        name: 'production',
        component: () => import('@/views/Production/ProductionIndexView.vue'),
        children: [
          {
            label: '产品列表',
            path: 'productionlist',
            name: 'productionlist',
            component: () => import('@/views/Production/ProductionListView.vue')
          },
          {
            label: '秒杀列表',
            path: 'seckilllist',
            name: 'seckilllist',
            component: () =>
              import('@/views/Production/ProductionSecKillView.vue')
          },
          {
            label: '筛选列表',
            path: 'categorylist',
            name: 'categorylist',
            component: () =>
              import('@/views/Production/ProductionCategoryView.vue')
          }
        ]
      },
      {
        label: '用户管理',
        path: 'user',
        name: 'user',
        component: () => import('@/views/User/UserIndexView.vue'),
        children: [
          {
            label: '用户列表',
            path: 'userlist',
            name: 'userlist',
            component: () => import('@/views/User/UserListView.vue')
          }
        ]
      }
    ]
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes // `routes: routes` 的缩写
})
const isLogin = () => {
  // console.log('localStorage------>', localStorage)
  // console.log('localStorage---getItem------>', localStorage.getItem)
  // onsole.log('localStorage------>', localStorage)
  return !!localStorage.getItem('token') //如果token为''|undefined|false等，返回结果都是false
  //获取指定key本地存储的值；   //  获取指定key 本地存储数据的值。
}
router.beforeEach((to) => {
  if (to.name === 'login') {
    console.log('路由守卫')
    if (isLogin()) {
      ElMessage('登录状态有效，不需要重复登录')
      return { name: 'home' }
    }
  }
  return true
})
export default router
